/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ru.vpitervip.app.client.place;

import java.util.List;

import ru.vpitervip.client.managed.request.FacilityProxy;
import ru.vpitervip.client.managed.request.MetroStationProxy;

import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceTokenizer;

/**
 * A place object representing a particular state of the UI. A Place can be converted to and from a
 * URL history token by defining a {@link PlaceTokenizer} for each {@link Place}, and the 
 * {@link PlaceHistoryHandler} automatically updates the browser URL corresponding to each 
 * {@link Place} in your app.
 */
public class FlatsPlace extends MenuItemPlace {
  
	/**
	 * Sample property (stores token). 
	 */
	private String name;
	private MetroStationProxy metroStation;
	private Integer rooms;
	private List<String> facilityIds;
	private Integer flatId;
	
	
	

	public FlatsPlace(String name) {
		this.name = name;
	}

	public FlatsPlace(MetroStationProxy metroStation, Integer rooms,
			List<String> facilityIds) {
		this.metroStation=metroStation;
		this.rooms=rooms;
		this.facilityIds=facilityIds;
	}

	public FlatsPlace(Integer flatId) {
		this.flatId=flatId;
	}

	public String getName() {
		return name;
	}
	
	

	public MetroStationProxy getMetroStation() {
		return metroStation;
	}

	public Integer getRooms() {
		return rooms;
	}

    

	public List<String> getFacilityIds() {
		return facilityIds;
	}

	
	@Override
	public boolean equals(Object obj) {
		
		if (obj==null) return false;
		
		boolean result=false;
		
		if (obj instanceof FlatsPlace){
			FlatsPlace place=(FlatsPlace)obj;
			if (place.getRooms()==null){
				if(rooms!=null) return false;
			}else{
				if (!place.getRooms().equals(rooms)) return false;
			}
			
			if (place.getMetroStation()==null){
				if(metroStation!=null) return false;
			}else{
				if (metroStation==null) return false;
				if (!place.getMetroStation().getId().equals(metroStation.getId())) return false;
			}
			
			
			if (place.getFacilityIds()==null){
				if(facilityIds!=null) return false;
			}else{
				if (facilityIds==null) return false;
				if (place.getFacilityIds().size()!=facilityIds.size()) return false;
				for (String facId:place.getFacilityIds()){
//					System.out.println(facId);
					if (!facilityIds.contains(facId)) return false;
			}
			}
			
			if (place.getFlatId()==null){
				if(flatId!=null) return false;
			}else{
				if (!place.getFlatId().equals(flatId)) return false;
			}
			
			result=true;
			
		}
		
		
		
		return result;
	}


	public Integer getFlatId() {
		return this.flatId;
	}


	/**
	 * PlaceTokenizer knows how to serialize the Place's state to a URL token.
	 */
	public static class Tokenizer implements PlaceTokenizer<FlatsPlace> {

		@Override
		public String getToken(FlatsPlace place) {
			return place.getName();
		}

		@Override
		public FlatsPlace getPlace(String token) {
			return new FlatsPlace(token);
		}

	}
}
